package br.fatec.servico;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MySQLEmpresaDAO implements IEmpresaDAO{
	private static final String cadastraEmpresa = "INSERT INTO empresa "
			+ "(codigo, cnpj, nome, nomeFantasia, endereco, telefone) VALUES (?,?, ?, ?,?,? ) ";
	private static final String excluiEmpresa = "DELETE FROM empresa WHERE "
			+ "cnpj = ? ";
	private static final String consultaEmpresa= "SELECT *  FROM empresa WHERE "
			+ "cnpj = ? ";
	private static final String consultaNome = "SELECT *  FROM empresa WHERE "
			+ "nome LIKE ? ";
	private static final String alterarDados = "UPDATE empresa SET nome = ? "
			+ ", endereco = ?, telefone=? WHERE cnpf = ? ";
	String msg = "";

	@Override
	public ResultSet consulta(String cnpj) {
		ResultSet rs = null;
		PreparedStatement stmt;
		try {

			stmt = MySQLDAOFactory.criaConexao().prepareStatement(consultaEmpresa);
			stmt.setString(1, cnpj);
			rs = stmt.executeQuery();
			// stmt.close();
		} catch (SQLException e) {
			
			msg = e.getMessage();
			System.out.println(msg);
		}
		return rs;
	}
	@Override
	public ResultSet consultaNome(String nome) {
		ResultSet rs = null;
		PreparedStatement stmt;
		try {

			stmt = MySQLDAOFactory.criaConexao().prepareStatement(consultaNome);
			stmt.setString(1, nome);

			rs = stmt.executeQuery();
			
			// stmt.close();
		} catch (SQLException e) {
			msg = e.getMessage();
		}
		return rs;
	}
	@Override
	public int cadastra(int codigo, String cnpj, String nome, String nomeFantasia, String endereco, String telefone) {
		PreparedStatement stmt;
		int rc = 0;
		try {
			stmt = MySQLDAOFactory.criaConexao().prepareStatement(cadastraEmpresa);
			stmt.setInt(1, codigo);
			stmt.setString(2, cnpj);
			stmt.setString(3, nome);
			stmt.setString(4, nomeFantasia);
			stmt.setString(5, endereco);
			stmt.setString(6, telefone);

			rc = stmt.executeUpdate();
			stmt.close();
		} catch (SQLException e) {
			msg = e.getMessage();
			System.out.println("metodo cadastra mysqlempresadao =" + e.getMessage());
		}
		return rc;
	}
	

	@Override
	public int exclui(String cnpj) {
		PreparedStatement stmt;
		int rc = 0;
		try {
			stmt = MySQLDAOFactory.criaConexao().prepareStatement(excluiEmpresa);
			stmt.setString(1, cnpj);
			rc = stmt.executeUpdate();
			
			stmt.close();
		} catch (SQLException e) {
			msg = e.getMessage();
		}
		return rc;
	}

	@Override
	public int altera(String cnpj, String nome, String endereco, String telefone) {
		PreparedStatement stmt;
		int rc = 0;
		try {
			stmt = MySQLDAOFactory.criaConexao().prepareStatement(alterarDados);
			
			stmt.setString(1, nome);
			stmt.setString(2, endereco);
			stmt.setString(3, telefone);
			stmt.setString(4, cnpj);
			rc = stmt.executeUpdate();
			stmt.close();
		} catch (SQLException e) {
			msg = e.getMessage();
		}
		return rc;
	}

	public String getMessage() {
		return msg;
	}
}
